/**
 * 
 */
package com.otrace.actions;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import antlr.StringUtils;

import com.otrace.forms.ClassFilterForm;
import com.otrace.service.VirtualMachineService;
import com.otrace.vo.ResultVO;
import com.sun.jdi.Method;
import com.sun.jdi.VirtualMachine;

/**
 * @author Prabhu KVN
 *
 */
public class ClassFilterAction extends Action {
	VirtualMachine vm;
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		long startTime = System.currentTimeMillis();
		System.out.println("Start - execute method in:"+this.getClass().getName());
		boolean success = false;
		Vector<String> includeClasses = new Vector<String>();
		
		ClassFilterForm classFilterForm = (ClassFilterForm)form;
		String filterByClass = classFilterForm.getFilterByClass();
		String classes[] = filterByClass.split(",");
		for(String cls:classes){
			includeClasses.add(cls);
				
		}
		if(filterByClass!=null && ! filterByClass.isEmpty()){
			HttpSession session  = request.getSession();
			vm = (VirtualMachine)session.getValue("JVM");
			if(vm!=null){
				VirtualMachineService vmService = new VirtualMachineService();
				List<ResultVO> resultList = vmService.enableEventCapture(vm,includeClasses);
				request.setAttribute("resultList", resultList);
				Set<Method> methodList = new HashSet<Method>();
				for(ResultVO vo: resultList){
					methodList.addAll(vo.getMethods());
				}
				request.setAttribute("methodList", methodList);
				/*if(resultList.get(0)!=null){
				request.setAttribute("methodList", resultList.get(0).getMethods());
				}*/
				
				success = true;
			}
			
		}
		
		long endTime = System.currentTimeMillis();
		System.out.println("Finish execute method: total time - "+(endTime-startTime));
		if(success){
			return mapping.findForward("success");
		}else{
			return mapping.getInputForward();
		}
	}

}
